# -*- coding: utf-8 -*-
"""
Created on 2022/11/14 上午4:17

@author: chenfei
"""

import sys
sys.path.append('..')
import re
import MyRpc.MyRpcSql as rpc
import MyRpc.MyDb as mydb
import json

def eliminate_comment(source):
    '''
    1、剔除单行注释
    '''
    source = re.sub(r'(?<=--)[\s\S]*?(?=\n)', '', source).replace('--\n', '');

    '''
    2、剔除多行注释
    '''
    source = re.sub(r'(?<=/\*)[\s\S]*?(?=\*/)', '', source);
    source = re.sub(r'/\*\*/', '', source);
    return source.strip();

def first():
    rs = rpc.run_sql("dafu", "show_msg('吴大富是大帅哥！');", "");
    print(rs);

def two():
    rs = rpc.run_sql("dafu", "SELECT SCHEMA_NAME FROM sys.SCHEMAS", "");
    print(rs);

def two_0():
    print(mydb.get_schemas());

def two_1():
    rs = json.loads("[['MY_META'], ['WUDAGUI'], ['PUBLIC'], ['WUDAFU'], ['SYS'], ['MYY']]");
    print(rs)

def two_2():
    print(mydb.get_tables('MY_META'));

def two_3():
    vs = mydb.read_column_meta('wulaoyao', 'MY_META', 'MY_CACHES');
    for k in vs:
        print(k)
    print(vs);
    print(len(vs));

def two_count():
    print(mydb.get_table_count('MY_META', 'MY_USERS_GROUP'));

def two_row():
    print(mydb.get_table_row('MY_META', 'MY_USERS_GROUP', 0, 50));

def two_4():
    vs = mydb.re_register('wudafu_group');
    print(len(vs));

def register_db():
    vs = mydb.register_db('wulaoyao', '_token');
    print(len(vs));

def get_line():
    group_name = 'wulaoyao';
    user_token = '_token';
    line = "create schema {0};add_user_group('{0}', '{1}', 'all', '{0}');".format(group_name, group_name + user_token);
    print(line);

def run_select():
    vs = mydb.run_select('dafu', 'SELECT TABLE_NAME, SCHEMA_NAME FROM MY_META.MY_SELECT_VIEWS', 0, 2);
    print(vs);

'''
判断字符串中是否有分号 ;
'''
def has_semicolon(line):
    lst = [];
    flag = None;
    for i in range(len(line)):
        if line[i] == "\"" and (flag is None or flag == '大'):
            if flag is None:
                flag = '大';
            if len(lst) > 0:
                lst.pop();
                if len(lst) == 0:
                    flag = None;
            else:
                lst.append(i);
        elif line[i] == "'" and (flag is None or flag == '小'):
            if flag is None:
                flag = '小';
            if len(lst) > 0:
                lst.pop();
                if len(lst) == 0:
                    flag = None;
            else:
                lst.append(i);
        elif line[i] == ';' and flag is None and i < len(line) - 1:
            return False;
    if flag is not None:
        return '字符串有错误！';
    return True;

def has_dic():
    sql = '吴大富';
    dic = {'name': '吴大富是大帅哥！'};
    lst = [1, '吴大富'];
    print(dic.__contains__('name_1'));
    print(isinstance(dic, dict));
    print(isinstance(lst, list));
    print(dic.get('name'));

def dawn_sql():
    sql = "add_year(10, '2000-01-05');";
    vs = mydb.run_dawn_sql('dafu', sql);
    print(vs);

def dawn_sql_1():
    sql = '''
    function helloWorld(msg:String)
{
    show_msg(msg);
}
helloWorld('Welcome to Dawn Sql！');
    ''';
    vs = mydb.run_dawn_sql('dafu', sql);
    print(vs);

if __name__=='__main__':
    #first();
    #two_0();
    #two_2();
    #two_3();
    #dawn_sql();
    #two_count();
    #two_row();
    #two_1();
    #two_4();
    #register_db();
    #run_select();
    #print(type(has_semicolon('12 "ab; cd" + a+b;')));
    #print(has_semicolon('12 "ab; cd" + a+b;'));
    #has_dic();
    dawn_sql_1();
    #get_line();
    pass;